/*
 * GDO GeometryFactory
 * 
 * initial implementation : 31.8.2007
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections.Generic;
using System.Text;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;

namespace Petra.Gis.DataServices.GDO
{
	public unsafe partial class GeometryFactory
	{
    /***********************************************
     * 28.8.2007
     * 
     * creates Point
     * from intergraph blob structure
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/
    private static GeometryBase binaryToPoint(byte[] vData, int arrayOffset, System.Type parentGeomType)
    {
      Point oGeom = new Point();
      CoordinateList Coords = new CoordinateList(vData, arrayOffset + 16, 1, Dimension.dim3D);
      oGeom.coordList = Coords;

      Guid ingrGUID;
      fixed (byte* bGUID = &vData[arrayOffset])
      {
        ingrGUID = (*((Guid*)bGUID));
      }
      //if orientation process angle copy into geometry
      if (ingrGUID == getGuid[GeometryType.OrientedPoint])
      {
        fixed (byte* bCoordListPtr = &vData[arrayOffset + 40])
        {
          double* dRotationPtr = (double*)bCoordListPtr;
          if (*dRotationPtr == 0)
          {
            oGeom.Rotation = 90;
            if (*dRotationPtr++ == 0)
              oGeom.Rotation = 0;
          }
          else
          {
            oGeom.Rotation = Math.Atan(*dRotationPtr / *dRotationPtr--);
            oGeom.Rotation = oGeom.Rotation * 180 / Math.PI;
            if (*dRotationPtr < 0)
              oGeom.Rotation = oGeom.Rotation + 180;
          }
        }
      }
      return oGeom;
    }






    /***********************************************
     * 28.8.2007
     * 
     * creates intergraph blob structure 
     * from Point
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/

    private static byte[] pointToBinary(GeometryBase geometry) { 
      int lBlobSize;
      Guid gGeomGUID;

      Point _tmpPoint = (Point)geometry;

      if (_tmpPoint.Rotation == 0)
      {
        lBlobSize = 16 + (24);
        gGeomGUID = getGuid[GeometryType.Point];
      }
      else{
        lBlobSize = 16 + (48);
        gGeomGUID = getGuid[GeometryType.OrientedPoint];
      }

      System.IO.MemoryStream oIMStream = new System.IO.MemoryStream(lBlobSize);
      System.IO.BinaryWriter mBWriter = new System.IO.BinaryWriter(oIMStream);

      oIMStream.Position = 0;
      mBWriter.Write(gGeomGUID.ToByteArray());

      mBWriter.Write(_tmpPoint.coordList.ToArray(Dimension.dim3D));

      if (_tmpPoint.Rotation != 0)
      {
        mBWriter.Write((double)System.Math.Cos(_tmpPoint.Rotation * (System.Math.PI / 180)));
        mBWriter.Write((double)System.Math.Sin(_tmpPoint.Rotation * (System.Math.PI / 180)));
        mBWriter.Write((double)0);
      }
      return oIMStream.ToArray();
    }
	}
}
